package com.tougutips.cookbook;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.tougutips.cookbook.dto.NewCookbookDto;
import com.tougutips.cookbook.entity.*;
import com.tougutips.cookbook.mapper.CookbookDifficultMapper;
import com.tougutips.cookbook.mapper.MaterialMapper;
import com.tougutips.cookbook.service.CookbookService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author MJ
 * description:
 * Date: 2022/7/21 -15:49
 * @Todo:
 */
@SpringBootTest
@Slf4j
public class test  extends BaseController {
    @Autowired
    private CookbookService cookbookService;

    @Autowired
    private CookbookDifficultMapper cookbookDifficultMapper;

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Test
    public void testRedisson() throws InterruptedException {

        String key = "aa:bb:cc:01";
        //可重入锁
        RLock rLock =redissonClient.getLock(key);

        Thread.sleep(100);

        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            //for循环创建线程争抢锁
            new Thread(()->{
                try{
                    // 尝试加锁，最多等待5秒，上锁以后10秒自动解锁
                    boolean res = rLock.tryLock(5,10, TimeUnit.SECONDS);
                    if(!res){
                        //抢锁失败
                        log.error("线程{}抢锁失败", finalI);
                    }else {
                        Thread.sleep(500);
                        log.info("线程{}执行完毕", finalI);
                        rLock.unlock();
                        log.info("线程{}解锁", finalI);
                    }

                }catch (Exception e){
                    log.error(e.toString());
                }
            }).start();
        }


        Thread.sleep(6000000);
    }

    @Test
    void testCookbook(){
        for (int i = 0; i < 10; i++) {
            Material material = new Material();
            material.setType("配料");
            material.setUnit("克");
            material.setCreateTime(new Date());
            material.setMaterial("大蒜"+i);
            material.setHeat("20"+i);
            materialMapper.insert(material);
        }
    }

    @Test
    void addCookbook()
    {
        NewCookbookDto newCookbookDto = new NewCookbookDto();
        Cookbook cookbook = new Cookbook();
        cookbook.setCookbookName("麻婆豆腐");
        cookbook.setCreateTime(new Date());
        cookbook.setCover("封面地址url");
        cookbook.setCategoryId(1231123123L);
        cookbook.setDifficultType(1550058050789400577L);
        cookbook.setCollectors(0);

        newCookbookDto.setCookbook(cookbook);
        List<CookbookMaterial> cookbookMaterials = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            CookbookMaterial cookbookMaterial = new CookbookMaterial();
            cookbookMaterial.setDosage(1+i);
            cookbookMaterial.setMaterialId(1550048165133643778L);
            cookbookMaterials.add(cookbookMaterial);
        }

        newCookbookDto.setCookbookMaterials(cookbookMaterials);
        List<CookbookStep> cookbookSteps = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            CookbookStep cookbookStep = new CookbookStep();
            cookbookStep.setStep("1");
            cookbookStep.setStepTime("20");
            cookbookStep.setImage("步骤url");
            cookbookSteps.add(cookbookStep);
        }
        newCookbookDto.setCookbookSteps(cookbookSteps);

        R r = cookbookService.addCookbook(newCookbookDto);


    }

    @Test
    void addDifficult()
    {
        CookbookDifficult cookbookDifficult = new CookbookDifficult();
        cookbookDifficult.setDifficult("简单");
        CookbookDifficult cookbookDifficult1 = new CookbookDifficult();
        cookbookDifficult1.setDifficult("中等");
        CookbookDifficult cookbookDifficult2 = new CookbookDifficult();
        cookbookDifficult2.setDifficult("困难");
        CookbookDifficult cookbookDifficult3 = new CookbookDifficult();
        cookbookDifficult3.setDifficult("大佬");
        cookbookDifficultMapper.insert(cookbookDifficult);
        cookbookDifficultMapper.insert(cookbookDifficult1);
        cookbookDifficultMapper.insert(cookbookDifficult2);
        cookbookDifficultMapper.insert(cookbookDifficult3);

    }
    @Test
    void testPage(){
        logger.info("查询菜谱信息");
        startPage();
        List<Cookbook> list = cookbookService.list();
        TableDataInfo dataTable = getDataTable(list);
    }

    
}
